//
//  HockeyCosom.m
//  tp1-gti350
//
//  Created by Coulon-Hamelin, Alexandre on 13-09-19.
//  Copyright (c) 2013 Coulon-Hamelin, Alexandre. All rights reserved.
//

#import "HockeyCosom.h"


@implementation HockeyCosom

-(id)init
{
    self.team1 = [[Team alloc] init];
    self.team2 = [[Team alloc] init];
    self.goals = [[NSMutableArray alloc] init];
    self.formatedGoalList = [[NSMutableArray alloc] init];
    self.formatedGoalString = [NSString string];
    
    self.team1.name = @"Équipe 1";
    self.team2.name = @"Équipe 2";
    self.currentPeriod = 1;
    
    return self;
}

-(BOOL)addPlayer:(int)teamNb AndPlayerLastName: (NSString*) lastName AndPlayerFirstName : (NSString*) firstName AndPlayerNumber: (int) number
{
    BOOL toReturn=true;
    Player *tempPlayer = [[Player alloc] initWithEverything:number AndFirstName:firstName AndLastName:lastName];
    bool checkNumber = true;
    
    if(number > 0)
    {
        if(teamNb == 1)
        {
            if(self.team1.players.count == 5)
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Nombre maximal de joueurs atteint" message:@"Il y a déjà 5 joueurs dans l'équipe" delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:nil];
                [alert show];
                toReturn = false;
            }
            else
            {
                for (int i=0; i < self.team1.players.count; i++)
                {
                    Player *tempPlayer1 = [self.team1.players objectAtIndex:i];
                    if(tempPlayer1.number == number)
                    {
                        checkNumber = false;
                    }
                }
                
                if(checkNumber)
                {
                    [self.team1.players addObject:tempPlayer];
                }
                else
                {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Numéro déjà utilisé" message:@"Le numéro est déjà utilisé!" delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:nil];
                    [alert show];
                    toReturn = false;
                }
            }
        }
        else
        {
            if(self.team2.players.count == 5)
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Nombre maximal de joueurs atteint" message:@"Il y a déjà 5 joueurs dans l'équipe" delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:nil];
                [alert show];
                toReturn = false;
            }
            else
            {
                for (int i=0; i < self.team2.players.count; i++)
                {
                    Player *tempPlayer1 = [self.team2.players objectAtIndex:i];
                    if(tempPlayer1.number == number)
                    {
                        checkNumber = false;
                    }
                }
                
                if(checkNumber)
                {
                    [self.team2.players addObject:tempPlayer];
                }
                else
                {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Numéro déjà utilisé" message:@"Le numéro est déjà utilisé!" delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:nil];
                    [alert show];
                    toReturn = false;
                }
            }
        }
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Numéro invalide" message:@"Le numéro doit être supérieur à 0!" delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:nil];
        [alert show];
        toReturn = false;
    }
    
    return toReturn;
}

-(void)addGoal:(int)teamNb AndScorer: (Player*) scorer AndPass1 : (Player*) pass1 AndPass2: (Player*) pass2
{
    Goal *goal;
    
    if(scorer != nil)
    {
        scorer.goals += 1;
        
        if(teamNb == 1)
        {
            goal = [[Goal alloc] initWithTeam:self.team1 AndPeriod:self.currentPeriod AndScorer:scorer];
        }
        else
        {
            goal = [[Goal alloc] initWithTeam:self.team2 AndPeriod:self.currentPeriod AndScorer:scorer];
        }
        
        if(pass1 != nil)
        {
            goal.pass1 = pass1;
            pass1.passes += 1;
        }
        
        if(pass2 != nil)
        {
            goal.pass2 = pass2;
            pass2.passes += 1;
        }
        
        [self.goals addObject:goal];
        [self.formatedGoalList addObject:[goal getFormatedString]];
        
        if ([self.goals count] == 0)
        {
            self.formatedGoalString = [self.formatedGoalString stringByAppendingString: [goal getFormatedString]];
        }
        else
        {
            self.formatedGoalString = [self.formatedGoalString stringByAppendingString: [NSString stringWithFormat:@"%@%@", @"\r",[goal getFormatedString]]];
        }
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"But sans joueur" message:@"Vous devez entrer le joueur qui a marqué" delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:nil];
        [alert show];
    }
}
-(int)getTeamScore:(int)teamNb
{
    int score = 0;
    if(self.goals.count > 0)
    {
        for (int i=0; i < self.goals.count; i++)
        {
            Goal *tempGoal = [self.goals objectAtIndex:i];
            if(tempGoal.team == self.team1 && teamNb == 1)
            {
                score +=1;
            }
            else if(tempGoal.team == self.team2 && teamNb ==2)
            {
                score +=1;
            }
        }
    }
    return score;
}

-(void)generateThreeStars
{
    self.threeStars = [[NSMutableArray alloc] init];
    
    
    if(self.currentPeriod == 3)
    {
        if(self.team1.players.count > 2 && self.team2.players.count > 2)
        {
            Player * pass1Tmp = [Player alloc];
            
            [self.threeStars addObject:pass1Tmp];
            [self.threeStars addObject:pass1Tmp];
            [self.threeStars addObject:pass1Tmp];
            
            if([self getTeamScore:1] > ([self getTeamScore:2]-2))
            {
                for (int i=0; i<self.team1.players.count; i++)
                {
                    
                    Player *tempPlayer = [self.team1.players objectAtIndex:i];
                    
                    Player *star1 = [self.threeStars objectAtIndex:0];
                    
                    Player *star2 = [self.threeStars objectAtIndex:1];
                    
                    Player *star3 = [self.threeStars objectAtIndex:2];
                    
                    if(tempPlayer.getPoints >= star1.getPoints)
                    {
                        if(tempPlayer.getPoints == star1.getPoints)
                        {
                            if(tempPlayer.goals > star1.goals)
                            {
                                [self addPlayerInThreeStars:1 :tempPlayer];
                            }
                            else if(tempPlayer.goals == star1.goals)
                            {
                                if(star1.number == 0)
                                {
                                    [self addPlayerInThreeStars:1 :tempPlayer];
                                }
                                else
                                {
                                    int rmd = arc4random() %2;
                                    if(rmd == 0)
                                    {
                                        [self addPlayerInThreeStars:1 :tempPlayer];
                                    }
                                    else
                                    {
                                        [self addPlayerInThreeStars:2 :tempPlayer];
                                    }
                                }
                            }
                            else
                            {
                                if(tempPlayer.getPoints >= star2.getPoints)
                                {
                                    if(tempPlayer.goals >= star2.goals)
                                    {
                                        [self addPlayerInThreeStars:2 :tempPlayer];
                                    }
                                }
                            }
                        }
                        else
                        {
                            [self addPlayerInThreeStars:1 :tempPlayer];
                        }
                        
                    }
                    else if(tempPlayer.getPoints >= star2.getPoints)
                    {
                        if(tempPlayer.getPoints == star2.getPoints)
                        {
                            if(tempPlayer.goals > star2.goals)
                            {
                                [self addPlayerInThreeStars:2 :tempPlayer];
                            }
                            else if(tempPlayer.goals == star2.goals)
                            {
                                if(star2.number == 0)
                                {
                                    [self addPlayerInThreeStars:2 :tempPlayer];
                                }
                                else
                                {
                                    int rmd = arc4random() %2;
                                    if(rmd == 0)
                                    {
                                        [self addPlayerInThreeStars:2 :tempPlayer];
                                    }
                                    else
                                    {
                                        [self addPlayerInThreeStars:3 :tempPlayer];
                                    }
                                }
                            }
                            else
                            {
                                if(tempPlayer.getPoints >= star3.getPoints)
                                {
                                    if(tempPlayer.goals >= star3.goals)
                                    {
                                        [self addPlayerInThreeStars:3 :tempPlayer];
                                    }
                                }
                            }
                        }
                        else
                        {
                            [self addPlayerInThreeStars:2 :tempPlayer];
                        }
                    }
                    else if(tempPlayer.getPoints >= star3.getPoints)
                    {
                        if(tempPlayer.goals > star3.goals)
                        {
                            [self addPlayerInThreeStars:3 :tempPlayer];
                        }
                        else if(tempPlayer.goals == star3.goals)
                        {
                            if(star3.number == 0)
                            {
                                [self addPlayerInThreeStars:3 :tempPlayer];
                            }
                            else
                            {
                                int rmd = arc4random() %2;
                                if(rmd == 0)
                                {
                                    [self addPlayerInThreeStars:3 :tempPlayer];
                                }
                            }
                        }
                    }
                }
            }
            
            if([self getTeamScore:2] > ([self getTeamScore:1]-2))
            {
                for (int j=0; j<self.team2.players.count; j++)
                {
                    
                    Player *tempPlayer = [self.team2.players objectAtIndex:j];
                    Player *star1 = [self.threeStars objectAtIndex:0];
                    
                    Player *star2 = [self.threeStars objectAtIndex:1];
                    
                    Player *star3 = [self.threeStars objectAtIndex:2];
                    
                    if(tempPlayer.getPoints >= star1.getPoints)
                    {
                        if(tempPlayer.getPoints == star1.getPoints)
                        {
                            if(tempPlayer.goals > star1.goals)
                            {
                                [self addPlayerInThreeStars:1 :tempPlayer];
                            }
                            else if(tempPlayer.goals == star1.goals)
                            {
                                if(star1.number == 0)
                                {
                                    [self addPlayerInThreeStars:1 :tempPlayer];
                                }
                                else
                                {
                                    int rmd = arc4random() %2;
                                    if(rmd == 0)
                                    {
                                        [self addPlayerInThreeStars:1 :tempPlayer];
                                    }
                                    else
                                    {
                                        [self addPlayerInThreeStars:2 :tempPlayer];
                                    }
                                }
                            }
                            else
                            {
                                if(tempPlayer.getPoints >= star2.getPoints)
                                {
                                    if(tempPlayer.goals >= star2.goals)
                                    {
                                        [self addPlayerInThreeStars:2 :tempPlayer];
                                    }
                                }
                            }
                        }
                        else
                        {
                            [self addPlayerInThreeStars:1 :tempPlayer];
                        }
                        
                    }
                    else if(tempPlayer.getPoints >= star2.getPoints)
                    {
                        if(tempPlayer.getPoints == star2.getPoints)
                        {
                            if(tempPlayer.goals > star2.goals)
                            {
                                [self addPlayerInThreeStars:2 :tempPlayer];
                            }
                            else if(tempPlayer.goals == star1.goals)
                            {
                                if(star2.number == 0)
                                {
                                    [self addPlayerInThreeStars:2 :tempPlayer];
                                }
                                else
                                {
                                    int rmd = arc4random() %2;
                                    if(rmd == 0)
                                    {
                                        [self addPlayerInThreeStars:2 :tempPlayer];
                                    }
                                    else
                                    {
                                        [self addPlayerInThreeStars:3 :tempPlayer];
                                    }
                                }
                            }
                            else
                            {
                                if(tempPlayer.getPoints >= star3.getPoints)
                                {
                                    if(tempPlayer.goals >= star3.goals)
                                    {
                                        [self addPlayerInThreeStars:3 :tempPlayer];
                                    }
                                }
                            }
                        }
                        else
                        {
                            [self addPlayerInThreeStars:2 :tempPlayer];
                        }
                    }
                    else if(tempPlayer.getPoints >= star3.getPoints)
                    {
                        if(tempPlayer.goals > star3.goals)
                        {
                            [self addPlayerInThreeStars:3 :tempPlayer];
                        }
                        else if(tempPlayer.goals == star3.goals)
                        {
                            if(star3.number == 0)
                            {
                                [self addPlayerInThreeStars:3 :tempPlayer];
                            }
                            else
                            {
                                int rmd = arc4random() %2;
                                if(rmd == 0)
                                {
                                    [self addPlayerInThreeStars:3 :tempPlayer];
                                }
                            }
                        }
                    }
                }
            }
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Pas assez de membres" message:@"Il n'y a pas assez de joueurs dans une ou les équipes." delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:nil];
            
            [alert show];
        }
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Fin de la partie" message:@"La période doit être la 3ième période pour finir une partie." delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:nil];
        
        [alert show];
    }
}

-(void)addPlayerInThreeStars:(int)index :(Player *)player
{
    Player *star1 = [self.threeStars objectAtIndex:0];
    Player *star2 = [self.threeStars objectAtIndex:1];
    
    [self.threeStars removeAllObjects];
    
    if(index == 1)
    {
        [self.threeStars addObject:player];
        [self.threeStars addObject:star1];
        [self.threeStars addObject:star2];
    }
    else if(index == 2)
    {
        [self.threeStars addObject:star1];
        [self.threeStars addObject:player];
        [self.threeStars addObject:star2];
    }
    else if(index == 3)
    {
        [self.threeStars addObject:star1];
        [self.threeStars addObject:star2];
        [self.threeStars addObject:player];
    }
}

- (Team *)getTeamByNumber :(int) number
{
    if (number == 1) {
        return self.team1;
    }
    else
    {
        return self.team2;
    }
}

- (BOOL) isValidTeamNumber:(int) number;
{
    if ((number ==1) || (number ==2))
    {
        return YES;
    }
    else
    {
        return FALSE;
    }
    
}

-(NSString *)getBestPlayersStringList
{
    NSString *stringToReturn = [[NSString alloc]init];
    
    for (int i=0 ; i < [self.threeStars count] ; i++) {
        
        Player *tpmPlayer = (Player *)[self.threeStars objectAtIndex:i] ;
        stringToReturn = [NSString stringWithFormat: @"%@ %@",stringToReturn ,[NSString stringWithFormat:@"%d %@",i+1,[tpmPlayer getPlayerResults]]];
    }
    
    return stringToReturn;
}

@end
